package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.weixing.mall.provider.mapper.MenuMapper;
import com.weixing.mall.provider.model.domain.Menu;
import com.weixing.mall.provider.model.dto.sys.SysMenuDto;
import com.weixing.mall.provider.model.enums.sys.SysFlag;
import com.weixing.mall.provider.model.query.SysMenuQuery;
import com.weixing.mall.provider.model.vo.sys.SysMenuTreeTableVo;
import com.weixing.mall.provider.model.vo.sys.SysMenuTreeVo;
import com.weixing.mall.provider.model.vo.sys.SysMenuVo;
import com.weixing.mall.provider.service.IMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统权限表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Override
    public List<SysMenuTreeTableVo> selectSysMenuTreeTableVo(SysFlag sysFlag) {
        SysMenuTreeTableVo rootMenuTreeVo = baseMapper.selectRoot();
        //查询出所有的menu
        List<SysMenuTreeTableVo> srcMenuTreeVo = baseMapper.selectMenuTreeTableVoAll(sysFlag.getValue());
        List<SysMenuTreeTableVo> distMenuTreeVo = Lists.newArrayList();
        //获取根目录
        fillSysMenuTreeTableVo(distMenuTreeVo,srcMenuTreeVo,rootMenuTreeVo);
        distMenuTreeVo.add(rootMenuTreeVo);
        return distMenuTreeVo;
    }

    @Override
    public List<SysMenuTreeVo> selectSysMenuTreeVo(SysFlag sysFlag) {
        SysMenuTreeTableVo rootMenuVo = baseMapper.selectRoot();
        SysMenuTreeVo rootMenuTreeVo = new SysMenuTreeVo();
        BeanUtils.copyProperties(rootMenuVo,rootMenuTreeVo);
        //查询出所有的menu
        List<SysMenuTreeVo> srcMenuTreeVo = baseMapper.selectMenuTreeVoAll(sysFlag.getValue());
        List<SysMenuTreeVo> distMenuTreeVo = Lists.newArrayList();
        //获取根目录
        fillSysMenuTreeVo(distMenuTreeVo,srcMenuTreeVo,rootMenuTreeVo);
        distMenuTreeVo.add(rootMenuTreeVo);
        return distMenuTreeVo;
    }

    @Override
    public List<SysMenuVo> selectSysMenuVo(SysMenuQuery sysMenuQuery) {
        return baseMapper.selectSysMenuVo(sysMenuQuery);
    }

    @Override
    public void save(SysMenuDto sysMenuDto) {
        Menu sysMenu = new Menu();
        BeanUtils.copyProperties(sysMenuDto,sysMenu);
        sysMenu.setIsEnable(sysMenuDto.getIsEnable().getValue());
        sysMenu.setMenuType(sysMenuDto.getMenuType().getValue());
        sysMenu.setVueIsLeaf(sysMenuDto.getIsLeaf().getValue());
        if (null == sysMenuDto.getId()){
            save(sysMenu);
        }else{
            updateById(sysMenu);
        }
    }

    /**
     * Description: 递归封装菜单Tree结构 <br/>
     *
     * @param:dist  目标集合
     * @param src  源集合
     * @param root  跟目录
     * @return:
     * @Author: kfc
     * @Date: 2019/5/17 10:26
     */
    private void fillSysMenuTreeTableVo(List<SysMenuTreeTableVo> dist, List<SysMenuTreeTableVo> src, SysMenuTreeTableVo root) {
        List<SysMenuTreeTableVo> menuTreeVoList = src.stream().filter((SysMenuTreeTableVo m1) -> m1.getParentId().longValue() == root.getId().longValue()).collect(Collectors.toList());
        if (!menuTreeVoList.isEmpty()) {
            menuTreeVoList.sort(new Comparator<SysMenuTreeTableVo>() {
                @Override
                public int compare(SysMenuTreeTableVo o1, SysMenuTreeTableVo o2) {
                    return o1.getOrderBy() - o2.getOrderBy();
                }
            });
            root.setChildren(menuTreeVoList);
//            root.setHasChildren(true);
            src.removeAll(menuTreeVoList);
            for (SysMenuTreeTableVo m : menuTreeVoList) {
                fillSysMenuTreeTableVo(dist, src, m);
            }
        }
    }

    private void fillSysMenuTreeVo(List<SysMenuTreeVo> dist, List<SysMenuTreeVo> src, SysMenuTreeVo root) {
        List<SysMenuTreeVo> menuTreeVoList = src.stream().filter((SysMenuTreeVo m1) -> m1.getParentId().longValue() == root.getId().longValue()).collect(Collectors.toList());
        if (!menuTreeVoList.isEmpty()) {
            menuTreeVoList.sort(new Comparator<SysMenuTreeVo>() {
                @Override
                public int compare(SysMenuTreeVo o1, SysMenuTreeVo o2) {
                    return o1.getOrderBy() - o2.getOrderBy();
                }
            });
            root.setChildren(menuTreeVoList);
            src.removeAll(menuTreeVoList);
            for (SysMenuTreeVo m : menuTreeVoList) {
                fillSysMenuTreeVo(dist, src, m);
            }
        }
    }
}
